$(function(){
    var auditInfo_datagrid,auditInfo_form,auditInfoSaveBtnAndCancelBtn,auditFailed_dialog,wtyBtn,orderBill_form,temporaryBtn;
    auditInfo_datagrid = $("#aduitInfo_datagrid");
    auditInfo_form = $("#auditInfo_form")
    auditInfoSaveBtnAndCancelBtn = $("#saveBtn,#cancelBtn");
    auditFailed_dialog = $("#auditFailed_dialog");
    orderBill_form = $("#orderBill_form");
    wtyBtn=$("#wtyBtn");
    temporaryBtn=$("#temporaryBtn");
    auditInfo_datagrid.datagrid({
        url:'/auditInfo/list',
        fit:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'emp',title:'核保人名称',width:10,align:'center',formatter:empFormatter},
            {field:'audit',title:'核保选项',width:10,align:'center',formatter:tabsFormatter},
            {field:'auditInsurance',title:'核保时间',width:10,align:'center',formatter:dateFormatter},
            {field:'opinion',title:'核保意见',width:10,align:'center',formatter:opinionFormatter}
        ]],
        onClickRow:function () {
            var rowData = auditInfo_datagrid.datagrid("getSelected");
            var admin = rowData.emp.admin;
            auditInfo_form.form("load",rowData);

            //判断当前记录中的状态,如果是同意或者退回了,就不能操作了
            if(rowData.auditTabs != 0 || admin == 1){
                //有审核时间就变灰
                auditInfoSaveBtnAndCancelBtn.linkbutton("disable");
            }else{
                //启用按钮
                auditInfoSaveBtnAndCancelBtn.linkbutton("enable");
            }
        },
        onLoadSuccess:function () {
            auditInfo_form.form("clear");
        }
    });

    auditFailed_dialog.dialog({
        title:"为什么?",
        buttons:$("#auditFailed_dialog_bt"),
        closed:true
    });

    $("#dialog_tips").dialog({
        title: '保单详细',
        width: 1000,
        height: 500,
        closed: true
    })


    var cmdObj={
        //新增
        save:function(){
            var rowData = auditInfo_datagrid.datagrid("getSelected");
            if (rowData) {
                var billId = rowData.bill.id;
                auditInfo_form.form("submit",{
                    url:"/auditInfo/pass?bill.id="+billId,
                    success:function(data){
                        data = $.parseJSON(data);
                        if(data.success){
                            //提示消息,当点确定的时候,关闭对话框,刷新数据表格
                            $.messager.alert("信息提示",data.msg,"warning",function(){
                                auditInfo_datagrid.datagrid("reload");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    }
                })
            } else {
                $.messager.alert("瞧我这暴脾气", "你丫要我弄谁呢?能不闹吗?", "warning");
            }
        },

        cancel:function () {
            var rowData = auditInfo_datagrid.datagrid("getSelected");
            if (rowData) {
                auditFailed_dialog.dialog("open");
            } else {
                $.messager.alert("瞧我这暴脾气", "你丫要我弄谁呢?能不闹吗?", "warning");
            }
        },

        //修改暂存状态
        temporary:function () {
            var rowData = auditInfo_datagrid.datagrid("getSelected");
            if (rowData) {
                var billId = rowData.bill.id;
                $.post("/orderBill/updateState?id="+billId+"&state=40",function(data){
                    if(data.success){
                        $.messager.alert("温馨提示",data.msg,"info",function () {
                            auditInfo_datagrid.datagrid("reload");
                        });
                    }else {
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                },"json")
            } else {
                $.messager.alert("瞧我这暴脾气", "你丫要我弄谁呢?能不闹吗?", "warning");
            }
        },


        wty:function () {
            var rowData = auditInfo_datagrid.datagrid("getSelected");
            var billId = rowData.bill.id;
            orderBill_form.form("submit",{
                url:"/orderBill/updateRemark?id="+billId+"&state=22",
                success:function(data){
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示消息,当点确定的时候,关闭对话框,刷新数据表格
                        $.messager.alert("温馨提示",data.msg,"info",function(){
                            auditFailed_dialog.dialog("close");
                            auditInfo_datagrid.datagrid("reload");
                            orderBill_form.form("clear");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            })
        },
        query:function () {
            var rowData = auditInfo_datagrid.datagrid("getSelected");
            if (rowData) {
                var billId = rowData.bill.id;
                $.get("/orderBill/queryDetailData?id=" + billId, function (selectedRow) {
                    //客户信息
                    selectedRow["realName"] = selectedRow.client.realName;
                    selectedRow["idCardType"] = selectedRow.client.idCardType;
                    selectedRow["idCardNumber"] = selectedRow.client.idCardNumber;
                    //承保信息表单
                    selectedRow["organizationId"] = selectedRow.organization.id;
                    selectedRow["organizationName"] = selectedRow.organization.organizationName;
                    selectedRow["productId"] = selectedRow.product.id;
                    selectedRow["productName"] = selectedRow.product.name;
                    selectedRow["rate"] = selectedRow.product.rate;
                    selectedRow["basicPrice"] = selectedRow.product.basicPrice;
                    selectedRow["paymentPrice"] = selectedRow.product.paymentPrice;
                    selectedRow["coveragePrice"] = selectedRow.product.coveragePrice;
                    //录入人
                    selectedRow["inputUserName"] = selectedRow.inputUser.name;
                    selectedRow["applyTime1"] = selectedRow.applyTime;
                    $("#applyTime1").datebox('setValue', '6/1/2012')
                    //车库汽车信息
                    selectedRow["sn"] = selectedRow.carInfo.sn;
                    selectedRow["carType"] = selectedRow.carInfo.carType;
                    selectedRow["basesNumber"] = selectedRow.carInfo.basesNumber;
                    selectedRow["airRelease"] = selectedRow.carInfo.airRelease;
                    selectedRow["seatNumber"] = selectedRow.carInfo.seatNumber;
                    selectedRow["rateDuality"] = selectedRow.carInfo.rateDuality;
                    selectedRow["mainChassis"] = selectedRow.carInfo.mainChassis;
                    selectedRow["engineNumber"] = selectedRow.carInfo.engineNumber;
                    selectedRow["newCarPrice"] = selectedRow.carInfo.newCarPrice;
                    selectedRow["importation"] = selectedRow.carInfo.importation;
                    selectedRow["power"] = selectedRow.carInfo.power;
                    //客户车辆信息
                    selectedRow["clientCarInfo.id"] = selectedRow.clientCarInfo.id;
                    selectedRow["hasCarNumber"] = selectedRow.clientCarInfo.hasCarNumber;
                    selectedRow["newCar"] = selectedRow.clientCarInfo.newCar;
                    selectedRow["foreignCar"] = selectedRow.clientCarInfo.foreignCar;
                    selectedRow["transferCar"] = selectedRow.clientCarInfo.transferCar;
                    selectedRow["carNumberType"] = selectedRow.clientCarInfo.carNumberType;
                    selectedRow["carNumber"] = selectedRow.clientCarInfo.carNumber;
                    selectedRow["registerTime"] = selectedRow.clientCarInfo.registerTime;
                    selectedRow["workingNature"] = selectedRow.clientCarInfo.workingNature;
                    selectedRow["workingArea"] = selectedRow.clientCarInfo.workingArea;
                    selectedRow["transferTime"] = selectedRow.clientCarInfo.transferTime;
                    selectedRow["driverInfo"] = selectedRow.clientCarInfo.driverInfo;
                    selectedRow["driverYears"] = selectedRow.clientCarInfo.driverYears;
                    selectedRow["fleet"] = selectedRow.clientCarInfo.fleet;
                    selectedRow["queryNumber"] = selectedRow.clientCarInfo.queryNumber;
                    selectedRow["relationToClient"] = selectedRow.clientCarInfo.relationToClient;
                    selectedRow["checkNumber"] = selectedRow.clientCarInfo.checkNumber;
                    $("#form_orderTips").form("load", selectedRow)
                    $("#dialog_tips").dialog("open")
                }, "json")
            } else {
                $.messager.alert("瞧我这暴脾气", "你丫要我弄谁呢?能不闹吗?", "warning");
            }
        }

    };


    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })




});


function empFormatter(value,row,index){
    if(row.emp.admin == 1){
        return "<font color=#6495ed>系统自核</font>"
    }else{
        return row.emp.name;
    }
}

function dateFormatter(value,row,index){
    return value? value.pushTime:"";
}


function tabsFormatter(value,row,index){
    if(row.emp.admin == 1){
        return "<font color=#6495ed>【自核转人工】</font>"
    }else{
        return "<font>提交核保</font>"
    }
}

function opinionFormatter(value,row,index){
    if(row.emp.admin == 1){
        return "<font color=#6495ed>【系统消息】:null</font>"
    }else{
        return row.opinion;
    }
}




